package com.aivin.firplugin.httplib;
import com.aivin.firplugin.bean.ApkInfo;
import com.aivin.firplugin.bean.AppDetail;
import com.aivin.firplugin.tool.WkLog;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;


public class NetTool {
    public NetTool( ){    }

    public void get(String url ,HttpRespondCallback httpRespondCallback){
        try {
            HttpGet httpGet = new HttpGet( url);
            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity, "UTF-8");
            httpRespondCallback.onGetInfo(responseString ,false);
        } catch (Exception e){
            httpRespondCallback.onGetInfo(e.getMessage() ,true);
        }
    }


    public void postMsg(String url ,  ArrayList<BasicNameValuePair> parameters ,HttpRespondCallback httpRespondCallback)  {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url );
        httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(parameters));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            String html = EntityUtils.toString(response.getEntity(),Charset.forName("utf-8"));
            httpRespondCallback.onGetInfo(html ,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void postApkFile(  AppDetail appDetail , ApkInfo apkInfo ,String changelog ,
                              ProgressListener progressListener  ){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    CustomMultiPartEntity multiPartEntity = new CustomMultiPartEntity(progressListener);

                    multiPartEntity.addPart("key", new StringBody(appDetail.getCert().getBinary() .getKey()));
                    multiPartEntity.addPart("token", new StringBody(appDetail.getCert().getBinary() .getToken()));
                    multiPartEntity.addPart("file", new FileBody( new File(apkInfo.getFilePath())));
                    multiPartEntity.addPart("x:name", new StringBody( apkInfo.getName() ,Charset.forName("UTF-8")));
                    multiPartEntity.addPart("x:version", new StringBody(apkInfo.getVersionName()));
                    multiPartEntity.addPart("x:build", new StringBody(apkInfo.getVersionCode()));
                    multiPartEntity.addPart("x:changelog", new StringBody(changelog,Charset.forName("UTF-8")));

                    HttpPost post = new HttpPost(appDetail.getCert().getBinary() .getUpload_url() );
                    post.setEntity(multiPartEntity);

                    HttpClient client = new DefaultHttpClient();
                    HttpResponse response = client.execute(post);
                    HttpEntity entity = response.getEntity();
                    String responseString = EntityUtils.toString(entity, "UTF-8");
                    WkLog.showMsg("文件上传结果="+ responseString);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        }).start();
    }


    public void postApkIcon(AppDetail appDetail , ApkInfo apkInfo , ProgressListener progressListener  ){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    CustomMultiPartEntity multiPartEntity = new CustomMultiPartEntity(progressListener);

                    multiPartEntity.addPart("key", new StringBody(appDetail.getCert().getIcon() .getKey()));
                    multiPartEntity.addPart("token", new StringBody(appDetail.getCert().getIcon() .getToken()));
                    multiPartEntity.addPart("file", new FileBody( new File(apkInfo.getIconLocalPath())));

                    long fileSize = multiPartEntity.getContentLength();
                    HttpPost post = new HttpPost(appDetail.getCert().getBinary() .getUpload_url() );
                    post.setEntity(multiPartEntity);


                    HttpClient client = new DefaultHttpClient();
                    HttpResponse response = client.execute(post);
                    HttpEntity entity = response.getEntity();
                    String responseString = EntityUtils.toString(entity, "UTF-8");
                    WkLog.showMsg("文件上传结果="+ responseString);
                } catch (Exception e) {
                    e.printStackTrace();

                }

            }
        }).start();
    }

}
